﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Grade.Entity;
using PowerCreator.LMS.Web.Grade;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_GradeManager_AddSkill : PowerCreator.LMS.Web.Base.WebBasePage
{
    GradeManager objGradeManager = new GradeManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}
        if (!IsPostBack)
        {
            InitPage();
            BinderDownloadType();
            int SkillID = 0;
            if (Request.QueryString["SkillID"] != null)
            {
                SkillID = int.Parse(Request.QueryString["SkillID"].ToString());
                if (SkillID != 0)
                {
                    ViewState["SkillID"] = SkillID.ToString();
                    BinderForm(SkillID);
                    dpdlst_Type.SelectedValue = objGradeManager.LoadSkill(int.Parse(ViewState["SkillID"].ToString())).FKSkillTypeId.ToString();
                    dpdlst_Type.Enabled = false;
                }
            }
        }
    }
    private void BinderDownloadType()
    {
        DataTable dt = new DataTable();
        
        dt = objGradeManager.QuerySkillType("");
        dpdlst_Type.DataSource = dt;
        dpdlst_Type.DataTextField = "SkillTypeName";
        dpdlst_Type.DataValueField = "SkillTypeID";
        dpdlst_Type.DataBind();
        //if (ViewState["SkillID"] != null)
        //{
            
        //    for (int i = 0; i < dpdlst_Type.Items.Count; i++)
        //    {
        //        if (dpdlst_Type.Items[i].Value == )
        //        {
        //            dpdlst_Type.Items[i].Selected = true;
        //            return;
        //        }
        //    }
        //}

    }
    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddJob_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobName.Text = GetLanageValueByKey("lbl_AddJob_JobName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobDescription.Text = GetLanageValueByKey("lbl_AddJob_JobDescription", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        //base.InitPage();

        lbl_InfoTitle_AddDownType.Text = GetLanageValueByKey("lab_AddSkill_SkillTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_DownTypeName.Text =GetLanageValueByKey("lab_AddSkill_SkillName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_Score.Text =GetLanageValueByKey("lab_AddSkill_SkillScore", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_SkillType.Text = GetLanageValueByKey("lab_AddSkill_SkillType", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void BinderForm(int id)
    {
        VOTSkill vo = new VOTSkill();
        vo = objGradeManager.LoadSkill(id);
        txt_DownTypeName.Text = vo.SkillName;
        txt_Score.Text = vo.SkillScore.ToString();
    }
    private bool CheckPageData()
    {
        string search = string.Format("SkillName = '{0}'", txt_DownTypeName.Text);
        if (ViewState["SkillID"] != null)
        {
            search += "and SkillID<>" + ViewState["SkillID"].ToString();
        }
        if (objGradeManager.CountSkill(search) != 0)
        {

            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);

            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddNewType_checkMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        if (!PowerCreator.LMS.Common.Utils.PageValidate.IsNumber(txt_Score.Text))
        {
            lbl_msg_Error.Text = GetLanageValueByKey("msg_Question_IsNum_msg1", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        if (int.Parse(txt_Score.Text) < 0)
        {
            lbl_msg_Error.Text = GetLanageValueByKey("msg_Question_IsNum_msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTSkill vo = new VOTSkill();

        if (ViewState["SkillID"] == null)
        {
            vo.SkillName = txt_DownTypeName.Text.Trim();
            vo.SkillScore =int.Parse(txt_Score.Text) ;
            vo.FKSkillTypeId = int.Parse(dpdlst_Type.SelectedValue);
            if (objGradeManager.AddSkill(vo))
            {
                VOTSkillType objVOTSkillType = objGradeManager.LoadSkillType(vo.FKSkillTypeId); ;
                objVOTSkillType.SkillTypeScore += vo.SkillScore;
                objGradeManager.UpdateSkillType(objVOTSkillType);
                string url = "ManagerSkill.aspx";
                Script.RedirectURL(url);
            }
            else
            {
                lbl_msg_Error.Text = GetLanageValueByKey("msg_CelueSelectQuest_shuruti24", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        }
        else
        {
            vo = objGradeManager.LoadSkill(int.Parse(ViewState["SkillID"].ToString()));
            vo.SkillName = txt_DownTypeName.Text.Trim();
            int NowScore = int.Parse(txt_Score.Text);
            int Score = NowScore-vo.SkillScore ;
            vo.SkillScore = NowScore;

            if (objGradeManager.UpdateSkill(vo))
            {
                VOTSkillType objVOTSkillType = objGradeManager.LoadSkillType(vo.FKSkillTypeId); ;
                objVOTSkillType.SkillTypeScore += Score;
                objGradeManager.UpdateSkillType(objVOTSkillType);
                string url = "ManagerSkill.aspx";
                Script.RedirectURL(url);
            }
            else
            {
                lbl_msg_Error.Text = GetLanageValueByKey("msg_CelueSelectQuest_shuruti24", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManagerSkill.aspx";
        Script.RedirectURL(url);
    }
}
